/* ***** BEGIN LICENSE BLOCK *****
 *   Version: GPL 2.0
 *
 * Bookstack extension: a queue implementation for bookmarks.
 * Copyright (C) 2007-2011  Adam Dane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Certain portions of the code are owned by The Mozilla Corporation.
 * These portions are relicensed as GPL 2.0 as per the option to do so stated
 * by the respective licenses of applicable projects.
 *
 * ***** END LICENSE BLOCK ***** */

"use strict";

var NSGetFactory;

(function () {
  var Cc = Components.classes,
      Ci = Components.interfaces,
      Cu = Components.utils,
      Cr = Components.results,
      bookstackHandlerObj,
      bookstackHandlerModule,
      nsISupports = Ci.nsISupports,
      nsICommandLineHandler = Ci.nsICommandLineHandler,
      nsIFactory = Ci.nsIFactory,
      nsIModule = Ci.nsIModule,
      nsIComponentRegistrar = Ci.nsIComponentRegistrar,
      nsICategoryManager = Ci.nsICategoryManager,

      clhBase = "@mozilla.org/commandlinehandler/general-startup;1?type=",
      contractID = clhBase + "bookstack",
      classID = Components.ID("{dd84f400-e09b-46cd-980f-34d987f2d25c}"),
      category = "m-bookstack";

  function bookstackHandler() {}

  try {
    Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  }
  catch (ex) {}

  bookstackHandler.prototype = {
    classID: classID,
    /* nsISupports */
    QueryInterface : function clh_QI(aIId) {
      if (aIId.equals(nsICommandLineHandler) ||
          aIId.equals(nsIFactory) ||
          aIId.equals(nsISupports)) {
        return this;
      }
      throw Cr.NS_ERROR_NO_INTERFACE;
    },

    /* nsICommandLineHandler */

    // Most of this file is boilerplate.  Here is the pertinent part.
    handle: function (aCommandLine) {
      var uristr, branch;
      if (aCommandLine.findFlag("bookstack-add", false) >= 0) {
        try {
          uristr = aCommandLine.handleFlagWithParam("bookstack-add", false);
          branch = Cc["@mozilla.org/preferences;1"].getService(
            Ci.nsIPrefService).getBranch("extensions.bookstack.");
          branch.setCharPref("commanduri", uristr);
          branch.setBoolPref("buttonupdate", true);
          aCommandLine.preventDefault = true;
        }
        catch (ex) {
          aCommandLine.handleFlag("bookstack-add", false);
          aCommandLine.preventDefault = true;
          Cu.reportError(
            "incorrect parameter passed to -bookstack-add via command line.");
        }
      }
    },

    // Adds the flag handler to the help output.
    helpInfo: "  -bookstack-add <uri>     Push the URI on stack,\n",

    /* nsIFactory */

    createInstance: function clh_CI(aOuter, aIId) {
      if (aOuter) {
        throw Cr.NS_ERROR_NO_AGGREGATION;
      }
      return this.QueryInterface(aIId);
    },

    lockFactory: function clh_lock(aLock) {}
  };

  bookstackHandlerObj = bookstackHandler.prototype;

  bookstackHandlerModule =  {
    classID: classID,
    category: category,
    contractID: contractID,
    QueryInterface: function mod_QI(aIId) {
      if (aIId.equals(nsIModule) ||
          aIId.equals(nsISupports)) {
        return this;
      }
      throw Cr.NS_ERROR_NO_INTERFACE;
    },

    getClassObject: function mod_gch(aCompMgr, aCId, aIId) {
      if (aCId.equals(this.classID)) {
        return bookstackHandlerObj.QueryInterface(aIId);
      }
    },

    registerSelf: function mod_regself(aCompMgr, aFileSpec, aLocation, aType) {
      aCompMgr.QueryInterface(nsIComponentRegistrar);
      aCompMgr.registerFactoryLocation(
        this.classID,
        "bookstackHandlerObj",
        this.contractID,
        aFileSpec,
        aLocation,
        aType);
      Cc["@mozilla.org/categorymanager;1"].getService(
        nsICategoryManager).addCategoryEntry(
          "command-line-handler", this.category, this.contractID, true, true);
    },

    unregisterSelf: function mod_unreg(aCompMgr, aLocation, aType) {
      aCompMgr.QueryInterface(nsIComponentRegistrar);
      aCompMgr.unregisterFactoryLocation(this.classID, aLocation);
      Cc["@mozilla.org/categorymanager;1"].getService(
        nsICategoryManager).deleteCategoryEntry(
          "command-line-handler", this.category);
    },

    canUnload: function (aCompMgr) {
      return true;
    }
  };

  NSGetFactory = XPCOMUtils.generateNSGetFactory([bookstackHandler]);
}());

